package com.manage.controller;

import com.beans.Neighborhood;
import com.beans.Photo;
import com.beans.SysUser;
import com.beans.page.PageList;
import com.commons.util.Constants;
import com.commons.util.StringUtil;
import com.manage.util.QiNiuUtil;
import com.manage.util.UserUtils;
import com.services.NeighborhoodService;
import com.services.PhotoService;
import com.services.SysUserService;
import com.services.impl.PhotoServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiangwei
 * @since 18/1/11
 */
@Controller
@RequestMapping("neighborhood")
public class NeighborhoodController extends BaseController {
    @Autowired
    private NeighborhoodService neighborhoodService;

    @Autowired
    private PhotoService photoService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 查询列表
     *
     * @param neighborhood
     * @return
     */
    @RequiresPermissions("neighborhood:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(Neighborhood neighborhood, HttpServletRequest request) {
        request.setAttribute("neighborhood", neighborhood);
        return "neighborhood/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @param neighborhood
     * @return
     */
    @RequiresPermissions("neighborhood:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList, Neighborhood neighborhood) {
        return neighborhoodService.queryPage(pageList, neighborhood);
    }

    /**
     * 新增修改
     *
     * @param neighborhood
     * @return
     */
    @RequiresPermissions("neighborhood:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(Neighborhood neighborhood, Model model) throws IOException {
        List<Photo> photoList = new ArrayList<Photo>();
        if (neighborhood.getNbhId() != null) {
            neighborhood = neighborhoodService.queryById(neighborhood.getNbhId());

            Photo photo = new Photo();
            photo.setRelationId(neighborhood.getNbhId());
            photo.setRelationType(Constants.twoLong);
            photo.setCategory(Constants.minus);

            photoList = photoService.queryList(photo);
        }

        QiNiuUtil.qiNiuUpload(model, null);
        List users = sysUserService.queryList(new SysUser());
        SysUser no=new SysUser();
        no.setUsername("无");
        no.setUserId(0L);
        users.add( 0,no);
        model.addAttribute("neighborhood", neighborhood);
        model.addAttribute("photoList", photoList);
        model.addAttribute("users", users);

        model.addAttribute("storeUrl", UserUtils.storeUrl);
        return "neighborhood/edit";
    }

    @RequiresPermissions("neighborhood:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(Neighborhood neighborhood, HttpServletRequest request,
                       @RequestParam(value = "imgFileIds", required = false) String[] imgFileIds,
                       @RequestParam(value = "imgFileDes", required = false) String[] imgFileDes) {
        int result = -1;
        neighborhood.setCreateBy(UserUtils.getUser().getUserId());
        neighborhood.setUpdateBy(UserUtils.getUser().getUserId());

        if (neighborhood.getNbhId() == null) {
            result = neighborhoodService.add(neighborhood);
        } else {
            result = neighborhoodService.update(neighborhood);
        }

        if (result > 0) {
            Photo photo = new Photo();
            //
            photo.setCreateBy(UserUtils.getUser().getUserId());
            photo.setUpdateBy(UserUtils.getUser().getUserId());
            photo.setRelationType(Constants.twoLong);//楼盘

            //删除和更新图片
            PhotoServiceImpl psi = new PhotoServiceImpl();
            psi.photo(photoService, neighborhood.getNbhId(), photo, imgFileIds, imgFileDes, null, null);
            QiNiuUtil.handleQUrl(imgFileIds);
        }

        return "redirect:list";
    }

    @RequiresPermissions("sys:user:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long nbhId, HttpServletRequest request) {
        Neighborhood neighborhood = new Neighborhood();
        neighborhood.setNbhId(nbhId);
        neighborhood.setUpdateBy(UserUtils.getUser().getUserId());
        int result = neighborhoodService.delete(neighborhood);

        if (result > 0) {
            //删除关联图片
            Photo photo = new Photo();
            photo.setUpdateBy(UserUtils.getUser().getUserId());
            photo.setRelationType(Constants.twoLong);//楼盘
            photo.setRelationId(nbhId);

        }
        //TODO  成功失败结果页面
        return result;
    }

    /**
     * 新增修改
     *
     * @param neighborhood
     * @return
     */
    @RequiresPermissions("neighborhood:detail")
    @RequestMapping("detail")
    public String detail(Neighborhood neighborhood, Model model) throws IOException {
        List<Photo> photoList = new ArrayList<Photo>();
        if (!StringUtil.isEmpty(neighborhood.getNbhId())) {
            neighborhood = neighborhoodService.queryById(neighborhood.getNbhId());

            Photo photo = new Photo();
            photo.setRelationId(neighborhood.getNbhId());
            photo.setRelationType(3l);
            photo.setCategory(Constants.minus);

            photoList = photoService.queryList(photo);
        }

        QiNiuUtil.qiNiuUpload(model, null);

        model.addAttribute("neighborhood", neighborhood);
        model.addAttribute("photoList", photoList);

        return "neighborhood/detail";
    }

    /**
     * 不存在  返回true 存在返回false,条件为空返回false
     *
     * @param neighborhood
     * @param request
     * @return
     */
    @RequestMapping("notExists")
    @ResponseBody
    public Object exists(Neighborhood neighborhood, HttpServletRequest request) {

        List<Neighborhood> neighborhoods = neighborhoodService.queryExists(neighborhood);

        if (StringUtil.isEmpty(neighborhood.getNbhId())) {
            return !(neighborhoods.size() > 0);
        } else if (neighborhoods.size() > 0) {
            Neighborhood nhb = neighborhoodService.queryById(neighborhood.getNbhId());
            if (nhb.getNbhName().equals(neighborhood.getNbhName())) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }
}
